package com.jams.goodox.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jams.goodox.R;
import com.jams.goodox.listen.OnPasswordInputFinish;

import java.util.ArrayList;
import java.util.Map;

/**
 * Belong to the Project —— MyPayUI
 * Created by WangJ on 2015/11/25 15:39.
 */
public class PasswordView extends RelativeLayout implements View.OnClickListener {
    Context context;

    private String strPassword;     //输入的密码
    private TextView[] tvList;      //用数组保存6个TextView，为什么用数组？
    private TextView[] tvInputList;      //用数组保存12个TextView
    private LinearLayout delete;      //用数组保存12个TextView


    //因为就6个输入框不会变了，用数组内存申请固定空间，比List省空间（自己认为）
    private ArrayList<Map<String, String>> valueList;    //有人可能有疑问，为何这里不用数组了？
    //因为要用Adapter中适配，用数组不能往adapter中填充

    private ImageView imgCancel;
    private TextView tvForget;
    private int currentIndex = -1;    //用于记录当前输入密码格位置

    public PasswordView(Context context) {
        this(context, null);
    }

    public PasswordView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        View view = View.inflate(context, R.layout.layout_popup_bottom, null);

        valueList = new ArrayList<Map<String, String>>();
        tvList = new TextView[6];
        tvInputList = new TextView[11];

        imgCancel = (ImageView) view.findViewById(R.id.img_cancel);
        imgCancel.setOnClickListener(this);

        tvForget = (TextView) view.findViewById(R.id.tv_forgetPwd);
        tvForget.setOnClickListener(this);

        tvList[0] = (TextView) view.findViewById(R.id.tv_pass1);
        tvList[1] = (TextView) view.findViewById(R.id.tv_pass2);
        tvList[2] = (TextView) view.findViewById(R.id.tv_pass3);
        tvList[3] = (TextView) view.findViewById(R.id.tv_pass4);
        tvList[4] = (TextView) view.findViewById(R.id.tv_pass5);
        tvList[5] = (TextView) view.findViewById(R.id.tv_pass6);

        tvInputList[0] = (TextView) view.findViewById(R.id.tv_input1);
        tvInputList[1] = (TextView) view.findViewById(R.id.tv_input2);
        tvInputList[2] = (TextView) view.findViewById(R.id.tv_input3);
        tvInputList[3] = (TextView) view.findViewById(R.id.tv_input4);
        tvInputList[4] = (TextView) view.findViewById(R.id.tv_input5);
        tvInputList[5] = (TextView) view.findViewById(R.id.tv_input6);
        tvInputList[6] = (TextView) view.findViewById(R.id.tv_input7);
        tvInputList[7] = (TextView) view.findViewById(R.id.tv_input8);
        tvInputList[8] = (TextView) view.findViewById(R.id.tv_input9);
        tvInputList[9] = (TextView) view.findViewById(R.id.tv_input_none);
        tvInputList[10] = (TextView) view.findViewById(R.id.tv_input0);
        delete = (LinearLayout) view.findViewById(R.id.tv_inputX);

        setView();

        addView(view);      //必须要，不然不显示控件
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_cancel:
                break;
            case R.id.tv_forgetPwd:
                break;
        }
    }

    private void setView() {
        delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentIndex - 1 >= -1) {      //判断是否删除完毕————要小心数组越界
                    tvList[currentIndex--].setText("");
                }
            }
        });
        for (int i = 0; i < tvInputList.length; i++) {
            final int finalI = i;
            tvInputList[i].setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (finalI < 11 && finalI != 9) {    //点击0~9按钮
                        if (currentIndex >= -1 && currentIndex < 5) {      //判断输入位置————要小心数组越界
                            tvList[++currentIndex].setText(tvInputList[finalI].getText().toString());
                        }
                    }
                }
            });
        }
    }

    //设置监听方法，在第6位输入完成后触发
    public void setOnFinishInput(final OnPasswordInputFinish pass) {
        tvList[5].addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 1) {
                    strPassword = "";     //每次触发都要先将strPassword置空，再重新获取，避免由于输入删除再输入造成混乱
                    for (int i = 0; i < 6; i++) {
                        strPassword += tvList[i].getText().toString().trim();
                    }
                    pass.inputFinish();    //接口中要实现的方法，完成密码输入完成后的响应逻辑
                }
            }
        });
    }

    /* 获取输入的密码 */
    public String getStrPassword() {
        return strPassword;
    }

    /* 暴露取消支付的按钮，可以灵活改变响应 */
    public ImageView getCancelImageView() {
        return imgCancel;
    }

    /* 暴露忘记密码的按钮，可以灵活改变响应 */
    public TextView getForgetTextView() {
        return tvForget;
    }

    /**
     * 存放控件
     */
    public final class ViewHolder {
        public TextView btnKey;
        public LinearLayout llAllLayout;
        public LinearLayout icon;
    }
}
